package com.extra_archive.project.iisu.model.detail.factory  
{
	import com.extra_archive.framework.utils.NumberUtils;
	import com.carlcalderon.arthropod.Debug;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;

	// @class Name :	ImageClipFactory.as
	// @author : 		d.mind (min hyuk,lim) (http://www.dmindproject.com)
	// @last update : 	2010. 5. 4.
	// @version :		v1.0
	// @description : 
	
	public class ImageClipFactory
	{
		private static var _instance : ImageClipFactory;
		private var _index : int;
		private var _total : int;
		private var _source : MovieClip;

	
		public function ImageClipFactory(enforcer : SingletonEnforcer)
		{
			_index = 0;
		}
	
		//--------------------------------------------------------------------------------------------
		//
		//								public function          
		//         
		//--------------------------------------------------------------------------------------------
		public static function getInstance() : ImageClipFactory
		{
			if(_instance == null) _instance = new ImageClipFactory(new SingletonEnforcer());
			return _instance;
		}
	
		public function setData(linkageName : String) : void
		{
			_source = getLibraryAsset(linkageName) as MovieClip;		
			_total = _source.totalFrames;	
		}

		public function hasNext() : Boolean
		{
			if(_source == null) 
			{
				Debug.warning("source가 null입니다.");
				return false;	
			}
			return _index < _total;
		}
		
		public function next():*
		{
			if(_source == null) 
			{
				Debug.warning("source가 null입니다.");
				return null;	
			}
			
			_source.gotoAndStop(int(NumberUtils.randomIntInRange(1, _total)));
			var bmpData : BitmapData = new BitmapData(_source.width, _source.height, true, 0x00ffffff);
			bmpData.draw(_source);
			var bmp : Bitmap = new Bitmap(bmpData, "auto", true);
			var container : Sprite = new Sprite();
			bmp.x = - Math.round(bmp.width / 2);
			bmp.y = - Math.round(bmp.height / 2);
			container.addChild(bmp);
			return container;
		}

		public function getTotal():int
		{
			return _total;	
		}

		
		//--------------------------------------------------------------------------------------------
		//
		//								private / protected function
		//         
		//--------------------------------------------------------------------------------------------
	
		private function getLibraryAsset(name : String) : DisplayObject
		{
			var cls : Class = getDefinitionByName(name) as Class;
			return new cls();		
		}	
	}
}

class SingletonEnforcer
{
}
